package com.lin.controller.user;


import cn.hutool.system.UserInfo;
import com.lin.base.Result;
import com.lin.entity.User;
import com.lin.security.util.SecurityUtil;
import com.lin.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/api/base")
@Slf4j
public class UserController {

    @Resource
    UserServiceImpl userServiceImpl;

    @GetMapping("/test")
    public String test() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getName();
        log.info("*************************{}",principal);
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        log.info("**************userId:{}",currentUserUid);
        return "test";
    }

    @PostMapping("/register")
    public Result userRegister(@RequestBody @Validated User user) {
        if (user == null) return Result.fail("注册失败");
        try {
           userServiceImpl.register(user);
        } catch (Exception e) {
           return Result.fail(e.getMessage());
        }
       return Result.ok("注册成功");
    }

    @GetMapping("/user/identity")
    public Result getIdentity() {
        return Result.ok(SecurityUtil.getCurUserIdentity(),"获取成功");
    }




}
